﻿using MK;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace AutoEditUI
{
    internal class MESWork
    {
        public static Dictionary<string, string> MesCodeDictionary { get; internal set; }
        public static Dictionary<string, string> RealMesCodeDictionary { get; internal set; }

        internal static void Init(MainWindow mainWindow)
        {
            InitMes(mainWindow, MainWindow.gNowPath + "MesCode.txt");

        }

        private static bool InitMes(MainWindow mMainWindow, string v)
        {


            if (!File.Exists(v))
            {
                LogHelper.Error(" No " + v);
                return false;
            }

            StreamReader sRead = new StreamReader(v);
            string nowPC = sRead.ReadToEnd();
            sRead.Close();

            StringReader sr = new StringReader(nowPC);
            String line;
            List<string> mListmList = new List<string>();
            while ((line = sr.ReadLine()) != null)
            {
                if (MESWork.MesCodeDictionary == null)
                {
                    MESWork.MesCodeDictionary = new Dictionary<string, string>();
                }
                string[] s_ar = line.Split('=');
                MESWork.MesCodeDictionary.Add(s_ar[0], s_ar[1].Trim());
            }
            if (MESWork.MesCodeDictionary == null)
            {
                return false;
            }
            return true;
        }

        internal static void Work(MainWindow mainWindow, string v)
        {
            loadmes(mainWindow, v);
            if (mainWindow.gUIModel.ItemIcons != null)
                foreach (ItemIcon itemIcon in mainWindow.gUIModel.ItemIcons)
                {

                }
            if (mainWindow.gUIModel.ItemBarcodes != null)
                foreach (ItemBarcode itemBarcode in mainWindow.gUIModel.ItemBarcodes)
                {
                    if (!string.IsNullOrEmpty(itemBarcode.MESBindStr))
                    {

                        if (MESWork.RealMesCodeDictionary.ContainsKey(itemBarcode.MESBindStr))
                        {
                            itemBarcode.ItemValue = MESWork.RealMesCodeDictionary[itemBarcode.MESBindStr];
                        }
                    }
                }
            if (mainWindow.gUIModel.ItemQRs != null)
                foreach (ItemQR itemQR in mainWindow.gUIModel.ItemQRs)
                {
                    if (!string.IsNullOrEmpty(itemQR.MESBindStr))
                    {

                        if (MESWork.RealMesCodeDictionary.ContainsKey(itemQR.MESBindStr))
                        {
                            itemQR.ItemValue = MESWork.RealMesCodeDictionary[itemQR.MESBindStr];
                        }
                    }
                }
            if (mainWindow.gUIModel.ItemRects != null)
                foreach (ItemRect itemRect in mainWindow.gUIModel.ItemRects)
                {
                }
            if (mainWindow.gUIModel.ItemTexts != null)
                foreach (ItemText itemText in mainWindow.gUIModel.ItemTexts)
                {
                    itemText.b_show = false;
                    itemText.Text_ = "";
                    if (!string.IsNullOrEmpty(itemText.MESBindStr))
                    {

                        if (MESWork.RealMesCodeDictionary.ContainsKey(itemText.MESBindStr))
                        {
                            itemText.b_show = true;
                            itemText.Text_ = MESWork.RealMesCodeDictionary[itemText.MESBindStr];
                        }
                        else
                        {

                            string xxx = MESWork.MesCodeDictionary[itemText.MESBindStr];
                            if (xxx.Contains("+"))
                            {
                                List<string> xxxs = xxx.Split('+').ToList<string>();
                                foreach (string item in xxxs)
                                {
                                    itemText.b_show = true;
                                    itemText.Text_ += " " + getvaluefromdic(item);
                                }
                            }
                        }

                    }
                }
            return;




        }

        private static string getvaluefromdic(string mESBindStr)
        {
            if (MESWork.RealMesCodeDictionary.ContainsKey(mESBindStr))
            {
                return MESWork.RealMesCodeDictionary[mESBindStr];
            }
            return null;
        }

        private static bool loadmes(MainWindow mainWindow, string v)
        {
            if (MESWork.RealMesCodeDictionary == null)
            {
                MESWork.RealMesCodeDictionary = new Dictionary<string, string>();
            }
            MESWork.RealMesCodeDictionary.Clear();

            if (!File.Exists(v))
            {
                LogHelper.Error(" No " + v);
                return false;
            }

            StreamReader sRead = new StreamReader(v);
            string nowPC = sRead.ReadToEnd();
            sRead.Close();

            StringReader sr = new StringReader(nowPC);
            String line;
            List<string> mListmList = new List<string>();
            while ((line = sr.ReadLine()) != null)
            {
                if (line.Contains("SET "))
                {
                    line = line.Replace("SET ", "");
                    line = line.Trim();
                    string preline = line.Substring(0, line.IndexOf("="));
                    string code = line.Substring(line.IndexOf("=") + 1, line.Length - line.IndexOf("=") - 1);

                    if (MESWork.MesCodeDictionary.ContainsValue(preline))
                    {
                        string firstKey = MESWork.MesCodeDictionary.FirstOrDefault(q => q.Value == preline).Key;
                        MESWork.RealMesCodeDictionary.Add(firstKey, code);
                    }
                }
            }
            return false;
        }
    }
}